import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-03
 * Time: 12:58
 */
public class MyStack implements IMyStack{
    private int[] elem;
    private int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    @Override
    public void push(int x) {
        //判断是否满，满了就扩容
        if(isFull() == true) {
            elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        elem[usedSize] = x;
        usedSize++;
    }

    @Override
    public int pop() {
        if(empty()) {
            //抛异常
            throw new MyStackException("栈空了");
        }
        int old = elem[usedSize - 1];
        usedSize--;
        return old;
    }

    @Override
    public int peek() {
        if(empty()) {
            //抛异常
            throw new MyStackException("栈空了");
        }
        return elem[usedSize - 1];
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public boolean empty() {
        return usedSize == 0;
    }

    @Override
    public boolean isFull() {
        if(usedSize == elem.length) {
            return true;
        }
        return false;
    }

    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.println(elem[i] + " ");
        }
        System.out.println();
    }
}
